﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CameraController : MonoBehaviour {

	public GameObject label;

	public Text poiText;

	private Vector3 initPos;

	private Quaternion initRot;

	private float t;

	private const float slerpFactor = 0.5f;

	// Use this for initialization
	void Start () {
		Input.location.Start ();
		Input.compass.enabled = true;
		Input.gyro.enabled = true;

		//摄像机位置
		initPos = transform.position;
		//摄像机角度
		initRot = transform.rotation;

		t = Time.time;

		Debug.Log ("camera init loc is " + initPos + ", initRot is " + initRot);

		Debug.Log ("CameraController Start event.");
	}


	public void Click() {
		Vector2 pos = Input.mousePosition;
		Debug.Log ("click 2Dpos is " + pos);

		Vector3 worldPos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10.0f));
		Debug.Log ("click 3Dpos is " + worldPos);
		
		try 
		{
			GameObject.Instantiate (label, worldPos, gameObject.transform.rotation);
		}
		catch(System.Exception e) 
		{
			Debug.LogError (e.Message);
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonUp (0)) {
			Click ();
		}
//		if (Time.time - t < 1.0f)
//			return;
//		t = Time.time;

//		float aspect = Camera.main.aspect;
//		Debug.Log("camera aspect" + aspect);

		//摄像机位置
		Vector3 pos = transform.position;
//		if (pos.x == 0 && pos.y == 0) 
//		{
//			transform.position = initPos;
//			pos = initPos;
//			transform.rotation = initRot;
//		}
		//摄像机角度
		Quaternion rot = transform.rotation;
		//获取摄像机x轴向的欧拉角
		float x = transform.eulerAngles.x;
		//获取摄像机y轴向的欧拉角
		float y = transform.eulerAngles.y;
		//获取摄像机z轴向的欧拉角
		float z = transform.eulerAngles.z;

		try {
			poiText.text = "";
			LocationInfo lti = Input.location.lastData;

			//Input.gyro.attitude 返回值为 Quaternion类型，即设备旋转欧拉角  
			Quaternion attitude = Input.gyro.attitude;
			//获取设备重力加速度向量  
			Vector3 deviceGravity = Input.gyro.gravity;  
			//设备的旋转速度，返回结果为x，y，z轴的旋转速度，单位为（弧度/秒）  
			Vector3 rotationVelocity = Input.gyro.rotationRate;  
			//获取更加精确的旋转  
			Vector3 rotationVelocity2 = Input.gyro.rotationRateUnbiased;  
			//设置陀螺仪的更新检索时间，即隔 0.1秒更新一次  
			Input.gyro.updateInterval = 0.1f;  
			//获取移除重力加速度后设备的加速度  
			Vector3 acceleration = Input.gyro.userAcceleration; 

			string res = 
				"经度:" + lti.longitude +
				";纬度:" + lti.latitude +
				";高度:" + lti.altitude +
				";罗盘度数:" + Input.compass.trueHeading +
				";陀螺仪欧拉旋转角:" + Input.gyro.attitude +
				";陀螺仪设备重力加速度向量:" + Input.gyro.gravity +
				";陀螺仪旋转速度:" + Input.gyro.rotationRate +
				";陀螺仪更加精确的旋转:" + Input.gyro.rotationRateUnbiased +
				";陀螺仪更新检索时间:" + Input.gyro.updateInterval +
				";陀螺仪移除重力加速度后设备的加速度:" + Input.gyro.userAcceleration +
//				";摄像机aspect:" + aspect + 
				";摄像机位置:" + pos + 
				";摄像机角度:" + rot + 
				";摄像机欧拉角:" + x + "," + y + "," + z;
			//		OnStart();
			//		poiText.text = jo.Call<string>("GetPoi", "酒店", "", 1);
			poiText.text = res;

			Debug.Log(res);

//			Vector3 newPos = new Vector3(pos.x + Input.gyro.userAcceleration.x, pos.y + Input.gyro.userAcceleration.y, pos.z + Input.gyro.userAcceleration.z);
//			transform.position = newPos;

//			transform.Translate(Input.gyro.userAcceleration);
//			for (int i = 0; i < Input.accelerationEventCount; i++) {
//				AccelerationEvent accEvent =  Input.accelerationEvents[i];
//				transform.Translate(accEvent.acceleration * accEvent.deltaTime);
//			}
//			transform.Translate(Input.acceleration);
			transform.rotation = Quaternion.Slerp(transform.rotation, ConvertRotation(Input.gyro.attitude), slerpFactor);

//			transform.rotation = Quaternion.AngleAxis(30, Vector3.up);

//			transform.Rotate(Vector3.up,Time.deltaTime*10f,Space.Self);
//			transform.Rotate(Vector3.left,Time.deltaTime*10f,Space.Self);
//			transform.Rotate(1, 1, 0);

			//摄像机位置 
//			float newPosX = pos.x + 1;
//			if (newPosX > initPos.x + 10) {
//				newPosX = initPos.x - 10;
//			}
//			Vector3 newPos = new Vector3(newPosX, pos.y, pos.z);
//			transform.position = newPos;
//			//摄像机角度
//			float newRotX = rot.x + 1;
//			float newRotY = rot.y + 1;
//			float newRotZ = rot.z + 1;
//			if (newRotX > initRot.x + 30) {
//				newRotX = initRot.x - 30;
//			}
//			if (newRotY > initRot.y + 30) {
//				newRotY = initRot.y - 30;
//			}
//			if (newRotZ > initRot.z + 30) {
//				newRotZ = initRot.z - 30;
//			}
//			Quaternion newRot = new Quaternion(newRotX, newRotY, rot.z, rot.w);
//			transform.rotation = newRot;

			//获取摄像机x轴向的欧拉角
//			transform.eulerAngles.x = transform.eulerAngles.x + 10;
		} catch (System.Exception ex) {
			Debug.LogError ("GetPoiInfo error:" + ex.Message);
		}

//		if(Input.touchCount > 0)
//		{
//			Debug.Log ("touch event.");
//			if (Input.touches [0].phase == TouchPhase.Began) {
//				Debug.Log ("touch Began event.");
//			}
//		}
	}

	private Quaternion ConvertRotation(Quaternion q)
	{
		return Quaternion.Euler(90, 0, 0) * (new Quaternion(-q.x, -q.y, q.z, q.w));
	}
}
